//
//  GDWelcomeViewController.swift
//  weibo
//
//  Created by user on 15/11/26.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

import SDWebImage

class GDWelcomeViewController: UIViewController {
    
    //属性
    //iconView底部约束
    private var iconViewBottomCon: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        prepareUI()
        
        if let avatar_large = GDUserAccount.loadUserAccount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        // 获取用户数据
        GDUserAccount.loadUserAccount()?.loadUserInfo()
    }
    
    
    /// 在用户看得到View的时候开始做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //做动画
        moveAnimateion()
    }
    
    ///头像动画
    func moveAnimateion() {
        // 使用autolayout布局之后,不要去修改view的frame,bounds,center,修改约束的值
        iconViewBottomCon?.constant = -500
        
        //弹簧效果动画
        // duration: 1.2
        // delay: 延时
        // usingSpringWithDamping: 弹簧效果明显程度 0 - 1
        // initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //使用autolayout，只需要更新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                print("动画完成")
//                AppDelegate.switchRootViewController(true)
                // 切换界面
                
                
                AppDelegate.switchRootViewController(true)
        }
        
        
    }
    

    private func prepareUI(){
        //添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        
        //背景图约束
        
        bkgImageView.ff_Fill(view)
//        //左右
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[biv]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgImageView]))
//        
//        //上下
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[biv]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : bkgImageView]))
        //头像约束
        
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        //获取某个约束
        iconViewBottomCon = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
//        //X
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        //Y要做动画，做全局约束
//        iconViewBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewBottomCon!)
//        
//        // 宽度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        
//        // 高度
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 欢迎lable
        
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
    }
    
    
    
    //懒加载控件
    //背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconView:UIImageView = {
       let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置圆角
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        
        return view
    }()
    
    //消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.sizeToFit()
        
        return label
    }()

}
